package org.oexam.ostudy.serv;

import java.io.IOException;

import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.oexam.ostudy.facade.TestSetPages;
import org.oexam.ostudy.facade.struct.InsertNewTestSetDetails;
import org.oexam.ostudy.facade.struct.LoggedUserDetails;

/**
 * Servlet implementation class AddNewTestSet
 */
@WebServlet("/AddNewTestSet")
public class AddNewTestSet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddNewTestSet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		LoggedUserDetails details = (LoggedUserDetails) request.getSession()
				.getAttribute("userLoginDetails");

		if (details == null) {
			response.sendRedirect("Login.jsp");
		}
		if (details.isAuthenticated() && (details.getUserType() == 0)) {

			response.sendRedirect("AddTestSet.jsp");

		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		LoggedUserDetails details = (LoggedUserDetails) request.getSession()
				.getAttribute("userLoginDetails");

		if (details == null) {
			response.sendRedirect("Login.jsp");
		}

		if (details.isAuthenticated() && (details.getUserType() == 0)) {

			InsertNewTestSetDetails insertNewTestSetDetails = new InsertNewTestSetDetails();
			insertNewTestSetDetails.setTestSet_name(request
					.getParameter("testSetName"));
			insertNewTestSetDetails.setNoOfTests(request
					.getParameter("noOfTests"));
			insertNewTestSetDetails.setTestSetMonth(request
					.getParameter("testSetMonth"));
			insertNewTestSetDetails.setTestSetYear(request
					.getParameter("testSetYear"));
			insertNewTestSetDetails.setActive(request.getParameter("isActive"));

			// try {
			// insertNewTestSetDetails.insert(insertNewTestSetDetails);
			// } catch (SQLException e) {
			// request.getSession().setAttribute("message",
			// "Insertion failed");
			//
			// response.sendRedirect("AddTestSet.jsp");
			// } catch (NamingException e) {
			// request.getSession().setAttribute("message",
			// "Insertion failed");
			//
			// response.sendRedirect("AddTestSet.jsp");
			// }

			String addResult = "";
			try {
				addResult = new TestSetPages()
						.addGivenTestSet(insertNewTestSetDetails);
			} catch (SQLException e) {

				e.printStackTrace();
				addResult.concat(e.getMessage());
			} catch (NamingException e) {

				e.printStackTrace();
				addResult.concat(e.getMessage());
			}

			if (addResult == null) {
				request.getSession().setAttribute("message",
						"Inserion has failed because of unknown reasons.");

				response.sendRedirect("AddTestSet.jsp");
			} else if (addResult.equalsIgnoreCase("ok")) {

				request.getSession().setAttribute(
						"message",
						"The Test set, " + insertNewTestSetDetails.testSet_name
								+ "   " + "Inserted Successfully");

				response.sendRedirect("Administrators.jsp");
			} else {
				request.getSession().setAttribute("message", addResult);

				response.sendRedirect("AddTestSet.jsp");
			}
		}

	}

}
